package ru.study.thinkin.chapter14;

import java.util.Iterator;

public class CoffeeGeneratorIterable extends CoffeeGenerator implements Iterable<Coffee>{	
	private int j;

	public CoffeeGeneratorIterable(int j) {
		this.j=j;
	}
	
	@Override
	public Iterator<Coffee> iterator() {	
		return new Iterator() {
			@Override
			public boolean hasNext() {
				return j>0;
			}
			
			@Override
			public Coffee next() {			
				j--;
				return CoffeeGeneratorIterable.this.next();				
			}
			
			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
			
		};
	}
	
	public static void main(String[] args) {
		for (Coffee c : new CoffeeGeneratorIterable(5)) {
			System.out.println(c);
		}
	}

}
